#include "drm-core.h"
#include "de_jpeg.h"
#include "debug.h"
#include "file_list.h"


#include "rga.h"
#include "RgaApi.h"
#include "im2d.h"


#include "librga.h"


#include <stdlib.h>

//**************************************************************************************

#define  LCD_WIDTH      (1920)
#define  LCD_HEIGHT     (1080)

//**************************************************************************************

//#define TEST_JPG_FILE  "/home/rpdzkj/waller/5-3.jpg"

#define TEST_JPG_FILE1  "/home/rpdzkj/waller/5-3-1.jpg"
#define TEST_JPG_FILE2  "/home/rpdzkj/waller/5-3-2.jpg"
#define TEST_JPG_FILE3  "/home/rpdzkj/waller/5-3-3.jpg"
#define TEST_JPG_FILE4  "/home/rpdzkj/waller/5-3-4.jpg"

///media/rpdzkj/E83A31773A31443A/tanganqi/121U558-36-62a.jpg

///media/rpdzkj/E83A31773A31443A/tanganqi/221J453-7-4N1.jpg

#define TEST_JPG_FILE5  "/media/rpdzkj/E83A31773A31443A/yuzijiang/231A947-32-25Z.jpg"

//**************************************************************************************

uint32_t cur_index = 0;

uint32_t color_table[6] = {RED,GREEN,BLUE,BLACK,WHITE,BLACK_BLUE};
//**************************************************************************************



int show_full_red(void)
{
	int i,j;

	for(j=0; j<buf.height; j++)
	{
		for(i = 0 ; i<buf.width;i++)
		{
			buf.vaddr[(j)*buf.width+(i)] = RED;
		}
	}
}

int show_full_green(void)
{
	int i,j;

	for(j=0; j<buf.height; j++)
	{
		for(i = 0 ; i<buf.width;i++)
		{
			buf.vaddr[(j)*buf.width+(i)] = GREEN;
		}
	}
}

int show_full_blue(void)
{
	int i,j;

	for(j=0; j<buf.height; j++)
	{
		for(i = 0 ; i<buf.width;i++)
		{
			buf.vaddr[(j)*buf.width+(i)] = BLUE;
		}
	}
}



int show_jpeg(struct jpeg_file *jf,int x ,int y)
{

	int i,j;
	uint32_t word;

	for(j=0; j<jf->height; j++){
		for(i = 0 ; i<jf->width;i++){
			if((j+y) < buf.height && (i+x)<buf.width){
				word = 0;
				word = (word | jf->buffer[(j*jf->width+i)*jf->Bpp+2]) | 
					   (word | jf->buffer[(j*jf->width+i)*jf->Bpp+1])<<8 | 
					   (word | jf->buffer[(j*jf->width+i)*jf->Bpp])<<16;
				buf.vaddr[(j+y)*buf.width+(x+i)] = word;
			}
			else
				continue;
		}
	}
}

int show_jpeg2(struct jpeg_file *jf)
{

	int i,j;
	int r = 0;
	int g = 0;
	int b = 0;

	int temp = 0;

	int x_scale = 0;
	int y_scale = 0;
	
	uint32_t word;
	//*****************************************************
	x_scale = jf->width/buf.width;

	log_i("jf->width=%d\n",jf->width);
	log_i("buf.width=%d\n",buf.width);
	log_i("x_scale  =%d\n",x_scale);

	//*****************************************************
	y_scale = jf->height/buf.height;

	log_i("jf->height=%d\n",jf->height);
	log_i("buf.height=%d\n",buf.height);
	log_i("y_scale   =%d\n",y_scale);
	//*****************************************************

	x_scale = 1;
	y_scale = 1;
	
	//*****************************************************


	log_i("x-max=%d\n",buf.width*x_scale);
	log_i("y-max=%d\n",buf.height*y_scale);

	
	

	for(j=0; j<buf.height; j++)
	{
		for(i = 0 ; i<buf.width;i++)
		{
			word = 0;

			temp = (j*jf->width*y_scale + i*x_scale)*jf->Bpp;

			r = (temp + 2);
			g = (temp + 1);
			b = (temp + 0);

#if 0
			if((j==0)&&(i<10)){
				
				log_i("temp=%d\n",temp);
				log_i("r:g:b=%d:%d:%d\n",r,g,b);
			}
#endif
			word = (word | jf->buffer[r])    | 
				   (word | jf->buffer[g])<<8 | 
				   (word | jf->buffer[b])<<16;
			buf.vaddr[(j)*buf.width+(i)] = word;
		}
	}

	
	log_i("show finish\n");

}


int show_jpeg3(struct jpeg_file *jf)
{

	int i,j;
	int r = 0;
	int g = 0;
	int b = 0;

	int ret = -1;

	int temp = 0;

	
	uint32_t word;

	char* src_buf = NULL;
    char* dst_buf = NULL;
	
    im_rect 		src_rect;
    im_rect 		dst_rect;

    rga_buffer_t 	src;
    rga_buffer_t 	dst;
    IM_STATUS 		STATUS;

	//**************************************************************************************

	memset(&src_rect, 0, sizeof(src_rect));
	memset(&dst_rect, 0, sizeof(dst_rect));
	memset(&src, 0, sizeof(src));
	memset(&dst, 0, sizeof(dst));

	//**************************************************************************************

	log_i("jf->width=%d\n",jf->width);
	log_i("buf.width=%d\n",buf.width);

	log_i("jf->height=%d\n",jf->height);
	log_i("buf.height=%d\n",buf.height);
	//**************************************************************************************
	//src 

	
	src = wrapbuffer_virtualaddr(jf->buffer, jf->width, jf->height, jf->Bpp);


	log_i("src.width =%d\n",src.width);
	log_i("src.height=%d\n",src.height);
	//**************************************************************************************
	//dst 
	if(dst_buf != NULL){
		free(dst_buf);
		dst_buf = NULL;
	}

    dst_buf = (char*)malloc(1920*1080*jf->Bpp);

	dst = wrapbuffer_virtualaddr(dst_buf, 1920, 1080, jf->Bpp);

	log_i("dst.width =%d\n",dst.width);
	log_i("dst.height=%d\n",dst.height);
	//**************************************************************************************




	//**************************************************************************************

	ret = imcheck(src, dst, src_rect, dst_rect);
	if (IM_STATUS_NOERROR != ret) {
		log_i("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
		return -1;
	}
	//*****************************************************


	STATUS = imresize(src, dst);
	
	log_i("resizing ....  %s\n",imStrError(STATUS));
	//**************************************************************************************

	for(j=0; j<buf.height; j++)
	{
		for(i = 0 ; i<buf.width;i++)
		{
			word = 0;

			temp = (j*jf->width + i)*jf->Bpp;

			r = (temp + 2);
			g = (temp + 1);
			b = (temp + 0);

#if 0
			if((j==0)&&(i<10)){
				
				log_i("temp=%d\n",temp);
				log_i("r:g:b=%d:%d:%d\n",r,g,b);
			}
#endif
			word = (word | ((uint32_t)dst.vir_addr+r))	 | 
				   (word | ((uint32_t)dst.vir_addr+g)<<8) | 
				   (word | ((uint32_t)dst.vir_addr+b)<<16);
			buf.vaddr[(j)*buf.width+(i)] = word;
		}
	}

	//**************************************************************************************

}


int show_jpeg4(const char *filename)
{

	int i,j;
	int a = 0;
	int r = 0;
	int g = 0;
	int b = 0;

	int temp = 0;

	int width  = LCD_WIDTH;
	int height = LCD_HEIGHT;

	int file_len = 0;

	FILE* fs = NULL;
	
	char* picture = NULL;
	
	uint32_t word;
	//*****************************************************

	fs=fopen(filename,"rb");
	if (!fs)
	{
		return 0;
	}

    fseek(fs, 0, SEEK_END);
    file_len = ftell(fs);
    fseek(fs, 0, SEEK_SET);

	
    printf("file_len = %d\n",file_len);

	picture = (char *)malloc(file_len);
    if(!picture){
        log_i("malloc error\n");
        fclose(fs);
        fs = NULL;
        return 0;
    }

	fread(picture,file_len,1,fs);

	fclose(fs);
	fs = NULL;	
	//*****************************************************


	//*****************************************************

	for(j=0; j<height; j++)
	{
		for(i = 0 ; i< width;i++)
		{
			word = 0;

#if 0 //argb888
			a = (*(picture + 0))&0x000000ff;
			r = (*(picture + 1))&0x000000ff;
			g = (*(picture + 2))&0x000000ff;
			b = (*(picture + 3))&0x000000ff;
			picture = picture + 4;
#elif 1 //rgb888
			r = 0;
			g = 0;
			b = 0;
			
			b= (*(picture + 0))&0x000000ff;
			g= (*(picture + 1))&0x000000ff;
			r= (*(picture + 2))&0x000000ff;			

			picture = picture + 3;
#endif

			if((j==0)&&(i<10)){
				log_i("a:r:g:b=%d:%d:%d:%d\n",a,r,g,b);
			}		

			word = ((r<<0)|(g<<8) |(b<<16));
			
			buf.vaddr[(j)*buf.width+(i)] = word;
		}
	}

	
	log_i("show finish\n");

}



int show_jpeg5(const char *data_buff,int w ,int h)
{

	int i,j;
	int a = 0;
	int r = 0;
	int g = 0;
	int b = 0;

	int temp = 0;

	int width  = w;
	int height = h;

	int x_start = 0;
	int y_start = 0;
	
	uint32_t word;
	//*****************************************************

	if(!data_buff)
		return 0;
	//*****************************************************

	x_start = (LCD_WIDTH  - width)/2;
	y_start = (LCD_HEIGHT - height)/2;

#if 0
	log_i("x_start=%d\n",x_start);
	log_i("y_start=%d\n",y_start);

	log_i("width =%d\n",width);
	log_i("height=%d\n",height);
#endif
	//*****************************************************
	for(i=0;i<LCD_HEIGHT;i++)
	{
		for(j=0;j<LCD_WIDTH;j++)
		{
			if((i >= y_start)&&(i < (y_start + height))&&(j >= x_start)&&(j < (x_start + width)))
			{
				b= (*(data_buff + 0))&0x000000ff;
				g= (*(data_buff + 1))&0x000000ff;
				r= (*(data_buff + 2))&0x000000ff;	
				
				data_buff = data_buff + 3;
			}
			else
			{
				r = 0;
				g = 0;
				b = 0;
			}

			word = ((r<<0)|(g<<8) |(b<<16));
			
			buf.vaddr[(i)*buf.width+(j)] = word;

		}
	}

	//*****************************************************
}


int decode_and_show_jpeg(char *filename)
{

	int ret;

	int i = 0;
	int j = 0;
	
	int w = 0;
	int h = 0;

	int v_w = 0;
	int v_h = 0;

	int pos = 0;

	int swap_flag_w = 0;
	int swap_flag_h = 0;
	
	char* out_buff  = NULL;
	char* swap_buff = NULL;

	struct jpeg_file jf;

	//log_i("filename = %s\n",filename);

	jf.fp= fopen(filename, "rb");
	if (jf.fp== NULL) {
		log_e("can not open file,%s\n",filename);
		return -1;
	}
	
	memset(jf.filename,0,sizeof(jf.filename));
	memcpy(jf.filename,filename,strlen(filename));


	ret =is_jpeg_file(filename,&jf);
	if(ret < 0 ){
		log_e("not jpeg file");
		
    	fclose(jf.fp);
		return -1;
	}

	ret = decode_jpeg(filename,&jf);
	if(ret < 0 ){
		log_e("decode jpeg wrong\n");
		fclose(jf.fp);
	}

#if 0
	log_i("jf.filename=%s\n",jf.filename);
	log_i("jf.width=%d\n",jf.width);
	log_i("jf.height=%d\n",jf.height);
	log_i("jf.Bpp=%d\n",jf.Bpp);
	log_i("jf.size=%d\n",jf.width*jf.height*jf.Bpp);
	log_i("jf.size=%d\n",jf.size);
#endif

	out_buff = (char*)malloc(LCD_WIDTH*LCD_HEIGHT*jf.Bpp);
	memset(out_buff,0x00,LCD_WIDTH*LCD_HEIGHT*jf.Bpp);

	if(jf.width%2){
		v_w = jf.width + 1;
		swap_flag_w = 1;
	}
	else{
		v_w = jf.width;
	}
	
	if(jf.height%2){
		v_h = jf.height + 1;
		swap_flag_h = 1;
	}
	else{
		v_h = jf.height;
	}

	if(swap_flag_w||swap_flag_h){
		swap_buff = (char*)malloc(v_w*v_h*jf.Bpp);
		memset(swap_buff,0x00,v_w*v_h*jf.Bpp);
		for(i = 0;i < v_h;i++)
		{
			if(swap_flag_w)
				memcpy(swap_buff + i*v_w*jf.Bpp,jf.buffer + i*jf.width*jf.Bpp ,jf.width*jf.Bpp);	
		}
	}else{
		swap_buff = jf.buffer;
	}
	
#if 0
	log_i("v_w=%d\n",v_w);
	log_i("v_h=%d\n",v_h);	
#endif

#if 0

	w = (LCD_WIDTH*jf.width/LCD_HEIGHT);

	h = (LCD_WIDTH*jf.height/LCD_HEIGHT);

	log_i("w  =%d\n",w );
	log_i("h  =%d\n",h );

	rga_control2(MODE_RESIZE,jf.buffer,jf.width,jf.height,jf.Bpp,
							out_buff,w,h,jf.Bpp);



#elif 1
	if((jf.width >= LCD_WIDTH)&&(jf.height >= LCD_HEIGHT))
	{
		//log_i("---1---\n");

#if 0	
		w = (jf.width*LCD_HEIGHT/jf.height);
		if(w > LCD_WIDTH){
			w = LCD_WIDTH;
		}
		h = LCD_HEIGHT;
#endif
		w = LCD_WIDTH;
		h = LCD_HEIGHT;

		rga_control2(MODE_RESIZE,swap_buff,v_w,v_h,jf.Bpp,
								out_buff,w,h,jf.Bpp);
	}
	else if((jf.width >= LCD_WIDTH)&&(jf.height < LCD_HEIGHT))
	{
		//log_i("---2---\n");
	
		w = (jf.width*LCD_HEIGHT/jf.height);
		h = LCD_HEIGHT;
		
		rga_control2(MODE_RESIZE,swap_buff,v_w,v_h,jf.Bpp,
								out_buff,w,h,jf.Bpp);
	

	}
	else if((jf.width < LCD_WIDTH)&&(jf.height >= LCD_HEIGHT))
	{
		//log_i("---3---\n");

		w = (jf.width*LCD_HEIGHT)/jf.height;
		h = LCD_HEIGHT;

#if 0
		w = jf.width;
		h = jf.width*LCD_HEIGHT/LCD_WIDTH;

		log_i("w  =%d\n",w );
		log_i("h  =%d\n",h );
#endif
		rga_control2(MODE_RESIZE,swap_buff,v_w,v_h,jf.Bpp,
								out_buff,w,h,jf.Bpp);

	}
	else 
	{
		//log_i("---4---\n");
	
		rga_control2(MODE_RESIZE,swap_buff,v_w,v_h,jf.Bpp,
								out_buff,jf.width,jf.height,jf.Bpp);
		
		w = jf.width;
		h = jf.height;
	
	}
#endif

	show_jpeg5(out_buff,w,h);
						
	if(out_buff){
		free(out_buff);
		out_buff = NULL; 
	}
	if(swap_flag_w||swap_flag_h){
		if(swap_buff){
			free(swap_buff);
			swap_buff = NULL; 
		}
	}

	
	fclose(jf.fp);
	free_jpeg(&jf);

	return 0;

}






int main(int argc, char **argv)
{
	int ret = -1;

	
	char file_full_path[1024];

	c_RkRgaInit();log_i("\n");
	
	log_i("rga_version=%s\n",querystring(RGA_VERSION));

	ret = drm_init();	
	if(ret < 0 ){
		log_e("drm_init fail\n");
		return -1;
	}

	
#if 0

	decode_and_show_jpeg(TEST_JPG_FILE5);
	sleep(2);//sec

	while(1);

	return 0;

#elif 0
	show_jpeg4("out0w1920-h1080-rgb888.bin");
	while(1);

	return 0;
#elif 0

	decode_and_show_jpeg(TEST_JPG_FILE);
	while(1);
	return 0;

#elif 0
	rga_control(MODE_RESIZE,IM_DOWN_SCALE);
	log_i("rga succeed.\n");
	
	return 0;
	
#elif 0
	show_jpeg4("out0w1920-h1080-rgba8888.bin");
	sleep(5);//sec

	return 0;
	
#elif 1

	search_picture_path(SCAN_PICTURE_PATH);
	get_filename_from_txt();
#endif

	while(1)
	{

		memset(file_full_path,0,sizeof(file_full_path));

		get_file_full_path_by_buff(cur_index,file_full_path);
		//get_file_full_path_by_fd(cur_index,file_full_path);

		if(Is_file_jpg(file_full_path))
		{
			decode_and_show_jpeg(file_full_path);
			
			sleep(6);//sec
		}

		srand(time(NULL));

		cur_index = rand()%file_list_get_num();

		//if(cur_index == 2)
		//	break;
		
		cur_index ++ ;
		if(cur_index >= file_list_get_num())
			cur_index = 0; 

		usleep(100);
	}



	drm_exit();


	return 0;

}
